namespace egret.socketio.engine {

    export interface RequestOption {
        method?: string;
        data?: string;
    }

    export class PollingXHR extends Polling {

        protected request(opts?: RequestOption): HttpRequest {

            opts = opts || { method: "GET" };

            let req: HttpRequest = new HttpRequest();
            req.open(this.uri(), opts.method);
            req.response
            return req;
        }

        protected doPoll(): void {
            console.debug("xhr poll");

            let req = this.request();
            req.addEventListener(egret.Event.COMPLETE, () => {
                let data = req.response;
                this.onData(data);
            }, this);
            req.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                this.onError("xhr poll error");
            }, this);
            req.responseType = egret.HttpResponseType.TEXT;
            req.send();
        }

        protected doWrite(data: string, fn: Function): void {
            let opts: RequestOption = {};
            opts.method = "POST";
            opts.data = data;

            let req = this.request(opts);
            req.addEventListener(egret.Event.COMPLETE, () => {
                fn();
            }, this);
            req.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                this.onError("xhr post error");
            }, this);
            req.send(opts.data);
        }
    }

}